package com.zkingsoft.services.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.config.Dictionary;
import com.zkingsoft.dao.ImgDao;
import com.zkingsoft.dao.ShowElementDao;
import com.zkingsoft.model.Img;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.ShowElement;
import com.zkingsoft.pojo.ShowElementImg;
import com.zkingsoft.services.ShowElementService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-04-22 08:43
 */
@Service("showElementService")
public class ShowElementServiceImpl implements ShowElementService {

	@Autowired
	private ShowElementDao showElementDao;
	@Autowired
	private ImgDao imgDao;

	@Override
	public int add(ShowElement showElement) {

		return showElementDao.insert(showElement);

	}

	@Override
	public int modify(ShowElement showElement) {

		return showElementDao.update(showElement);

	}

	@Override
	public int remove(List<Long> list) {

		return showElementDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long id) {

		return showElementDao.deleteById(id);

	}

	@Override
	public List<ShowElement> findInPage(ShowElement showElement, PaginationVO pageVo) {

		return showElementDao.selectInPage(showElement, pageVo);

	}

	@Override
	public List<ShowElement> findByModel(ShowElement showElement) {

		return showElementDao.selectByModel(showElement);

	}

	@Override
	public int findTotal(ShowElement showElement) {

		return showElementDao.selectTotalRecord(showElement);

	}

	@Override
	public ShowElement findById(Long id) {

		return showElementDao.selectById(id);

	}

	@Override
	@Transactional
	public int addPrudct(ShowElementImg showElementImg) {
		int i = showElementDao.insert(showElementImg);
		// 插入图片部分
		List<Img> imgList = showElementImg.getImgs();
		if (imgList != null) {
			for (Img img2 : imgList) {
				if (img2 != null && img2.getPath() != null && !img2.getPath().equals("")) {
					img2.setTargetId(showElementImg.getId());
					img2.setTime(new Date());
					img2.setCompanyId(showElementImg.getCompanyId());
					img2.setType(Dictionary.ELEMENT_TYPE_PRODUCT);
					imgDao.insert(img2);
				}
			}
		}
		return i;
	}

	@Override
	@Transactional
	public int modifyPrudct(ShowElementImg showElementImg) {
		int i = 0;
		ShowElement showElement = showElementDao.selectById(showElementImg.getId());
		// 删除原来是图片
		imgDao.deleteByShowElement(showElement);
		i = showElementDao.update(showElementImg);
		List<Img> imgList = showElementImg.getImgs();
		// 添加新图片
		if (imgList != null) {
			for (Img img2 : imgList) {
				if (img2 != null && img2.getPath() != null && !img2.getPath().equals("")) {
					img2.setTargetId(showElement.getId());
					img2.setTime(new Date());
					img2.setCompanyId(showElement.getCompanyId());
					img2.setType(Dictionary.ELEMENT_TYPE_PRODUCT);
					imgDao.insert(img2);
				}
			}
		}
		return i;
	}

	@Override
	public int removePruduct(List<Long> ids) {
		int i = 0;
		ShowElement showElement = showElementDao.selectById(ids.get(0));
		// 删除原来的图片
		for (Long id : ids) {
			showElement.setId(id);
			imgDao.deleteByShowElement(showElement);
		}
		i = showElementDao.deleteByIds(ids);
		return i;
	}

	@Override
	public int addAlbum(ShowElementImg showElementImg) {
		int i = showElementDao.insert(showElementImg);
		// 插入图片部分
		List<Img> imgList = showElementImg.getImgs();
		if (imgList != null) {
			for (Img img2 : imgList) {
				if (img2 != null && img2.getPath() != null && !img2.getPath().equals("")) {
					img2.setTargetId(showElementImg.getId());
					img2.setTime(new Date());
					img2.setCompanyId(showElementImg.getCompanyId());
					img2.setType(Dictionary.ELEMENT_TYPE_ALBUM);
					imgDao.insert(img2);
				}
			}
		}
		return i;
	}

	@Override
	@Transactional
	public int modifyAlbum(ShowElementImg showElementImg) {
		int i = 0;
		ShowElement showElement = showElementDao.selectById(showElementImg.getId());
		// 删除原来是图片
		imgDao.deleteByShowElement(showElement);
		i = showElementDao.update(showElementImg);
		List<Img> imgList = showElementImg.getImgs();
		// 添加新图片
		if (imgList != null) {
			for (Img img2 : imgList) {
				if (img2 != null && img2.getPath() != null && !img2.getPath().equals("")) {
					img2.setTargetId(showElement.getId());
					img2.setTime(new Date());
					img2.setCompanyId(showElement.getCompanyId());
					img2.setType(Dictionary.ELEMENT_TYPE_ALBUM);
					imgDao.insert(img2);
				}
			}
		}
		return i;
	}
	
	
	@Override
	public List<ShowElement> findByCateName(String cateName, Long currentCompany) {
		return showElementDao.selectByCateName(cateName,currentCompany);
	}

	@Override
	public List<ShowElement> findByTime(HashMap<String, Object> query) {
		return showElementDao.selectByTime(query);
	}
	
}